package com.xin.ruanjian.omsystem.bases;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xin.ruanjian.omsystem.R;
import com.xin.ruanjian.omsystem.utils.ToastUtils;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * Created by local123 on 2017/12/31.
 */

public abstract class BaseFragment extends Fragment {
    protected static String TAG = "BaseFragment";

    public Toolbar mToolbar;
    public TextView mTvCenterTitle;
    public View view;
    //控件是否已经初始化
    private boolean isCreateView = false;
    protected AppCompatActivity context;

    private Unbinder unbinder;
    private SweetAlertDialog waitDialog;

    private boolean dialogIsCancelable = true;
    protected boolean isNeedReload = true;

    public BaseFragment() { /* compiled code */ }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context = (AppCompatActivity) getActivity();
        TAG = getClass().getSimpleName();
        if (null != view) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (null != parent) {
                parent.removeView(view);
            }
        } else {
            view = initContentView(inflater, container, savedInstanceState);
            unbinder = ButterKnife.bind(this, view);
        }

        mToolbar = view.findViewById(R.id.toolbar);

        initTitle(view);

        initView(view);
        isCreateView = true;
        isNeedReload = true;
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    protected void showLoadingDialog() {
        if (waitDialog == null) {
            waitDialog = new SweetAlertDialog(context, SweetAlertDialog.PROGRESS_TYPE);
            waitDialog.setCancelable(dialogIsCancelable);
        }
        if (waitDialog.getAlerType() != SweetAlertDialog.PROGRESS_TYPE) {
            waitDialog.changeAlertType(SweetAlertDialog.PROGRESS_TYPE);
        }
        waitDialog.setTitleText("加载中...");
        waitDialog.setContentText("");
        waitDialog.show();
    }

    protected void showErrorDialog() {
        if (waitDialog == null) {
            waitDialog = new SweetAlertDialog(context, SweetAlertDialog.ERROR_TYPE);
        }
        waitDialog.setTitleText("出错了")
                .setContentText(getString(R.string.please_check_your_network_connection))
                .setConfirmText("确定")
                .changeAlertType(SweetAlertDialog.ERROR_TYPE);
        waitDialog.setCancelable(dialogIsCancelable);
        waitDialog.show();
    }

    protected void showErrorDialog(String content) {
        if (waitDialog == null) {
            waitDialog = new SweetAlertDialog(context, SweetAlertDialog.ERROR_TYPE);
            waitDialog.setCancelable(dialogIsCancelable);
        }
        waitDialog.setTitleText("出错了")
                .setContentText(content)
                .setConfirmText("确定")
                .changeAlertType(SweetAlertDialog.ERROR_TYPE);
        waitDialog.show();
    }

    protected void showErrorDialog(int resId) {
        if (waitDialog == null) {
            waitDialog = new SweetAlertDialog(context, SweetAlertDialog.ERROR_TYPE);
            waitDialog.setCancelable(dialogIsCancelable);
        }
        waitDialog.setTitleText("出错了")
                .setContentText(getString(resId))
                .setConfirmText("确定")
                .changeAlertType(SweetAlertDialog.ERROR_TYPE);
        waitDialog.show();
    }

    protected void hideLoadingDialog() {
        if (waitDialog != null) {
            waitDialog.dismiss();
        }
    }

    //此方法在控件初始化前调用，所以不能在此方法中直接操作控件会出现空指针
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && isCreateView && isNeedReload) {
            initData();
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //第一个fragment会调用
        if (getUserVisibleHint())
            initData();
    }

    // 初始化标题栏
    protected void initTitle(View view) {
        mTvCenterTitle = view.findViewById(R.id.tv_centerTitle);
    }

    // 初始化UI setContentView
    protected abstract View initContentView(LayoutInflater inflater, @Nullable ViewGroup container,
                                            @Nullable Bundle savedInstanceState);

    // 初始化控件
    protected abstract void initView(View view);

    // 逻辑处理
    protected abstract void initData();

    protected void setTvCenterTitle(String title) {
        mTvCenterTitle.setText(title);
    }

    protected void setTvCenterTitle(int resId) {
        mTvCenterTitle.setText(resId);
    }

    protected void setTvCenterTitleColor(int resId) {
        mTvCenterTitle.setTextColor(context.getResources().getColor(resId));
    }

    protected void Toast(String text) {
        ToastUtils.showToast(text, context);
    }

    protected void Toast(int textId) {
        ToastUtils.showToast(textId, context);
    }
}
